package org.example.Pojo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import java.util.Locale;

public class GameScore {
    private Timer timer;//计时器
    private JLabel timelabel;//时间标签
    private JLabel step;//步数标签
    private int fact;//实际步数
    private int time;//限制的时间
    public boolean arrival;//是否到达限时的时间
    private int timeScore;//时间分数
    private int seconds=0;
    private int minutes=0;
    private int hours=0;


    public GameScore(int time){
        //是否到达限时的时间初始化
        arrival=false;
        this.time=time;//初始化限时的时间
        //时间标签
        timelabel=new JLabel();
        timelabel.setText("00:00:00");
        //计时器启动
        timer=new Timer(1000,new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                seconds++;
                if(seconds>=60){
                    minutes++;
                    seconds=0;
                }
                if(hours>=60){
                    hours++;
                    seconds=0;
                }
                timelabel.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
                if(time!=0)//==0的时候不限时不需要判断
                    arrival=judgetime(time,minutes,hours);
            }
        });
        timer.start();//启动计时器
        //步数初始化
        fact=0;
        step=new JLabel(Integer.toString(fact));
        step.setFont(new Font("宋体", Font.PLAIN, 24));
        //时间总分初始化
        timeScore=0;
    }

    //判断是否到达限时的时间
    public boolean judgetime(int time,int minutes,int hours) {
        int LimitHour=time/60;
        int LimitMinutes=time%60;
        return minutes == LimitMinutes && hours == LimitHour;
    }

    public int getFact() {
        return fact;
    }
    public JLabel getStep() {
        return  step;
    }

    public JLabel getTimelabel() {
        return timelabel;
    }

    public LocalTime getLocalTime(){
        return LocalTime.parse(timelabel.getText());
    }
    public Timer getTimer() {
        return timer;
    }

    public void setFact(int fact) {
        this.fact = fact;
    }

    public int getTimeScore(){
        switch (((hours * 60 + minutes) / 10) * 10) {
            case 0 -> timeScore = 100;
            case 10 -> timeScore = 90;
            case 20 -> timeScore = 80;
            case 30 -> timeScore = 70;
            case 40 -> timeScore = 60;
            case 50 -> timeScore = 50;
            case 60 -> timeScore = 40;
            case 70 -> timeScore = 30;
            case 80 -> timeScore = 20;
            case 90 -> timeScore = 10;
        }
        return timeScore;
    }
}

